package org.esgi.repository.impl;

import java.io.File;
import java.io.FilenameFilter;

import java.util.ArrayList;
import java.util.List;

import org.esgi.model.interfaces.IForm;

import org.esgi.repository.interfaces.IFormRepository;

import org.esgi.transformer.impl.XMLAnswerTransformer;
import org.esgi.transformer.impl.XMLFormTransformer;
import org.esgi.transformer.impl.XMLQuestionTransformer;
import org.esgi.transformer.interfaces.IXMLFormTransformer;

import org.esgi.util.RandomUniqueId;

import org.w3c.dom.Document;

public class XMLFormRepository extends XMLRepository implements IFormRepository
{
	private File directory_;
	private IXMLFormTransformer formTransformer_;
	
	public XMLFormRepository(File dir)
	{
		super();
		this.setDirectory(dir);
		String namespace = "bmb";
		XMLAnswerTransformer answerTransformer = new XMLAnswerTransformer();
		answerTransformer.setDefaultNamespace(namespace);
		XMLQuestionTransformer questionTransformer = new XMLQuestionTransformer(answerTransformer);
		questionTransformer.setDefaultNamespace(namespace);
		XMLFormTransformer formTransformer = new XMLFormTransformer(questionTransformer);
		formTransformer.setDefaultNamespace(namespace);
		formTransformer_ = formTransformer;
	}
	
	public void setDirectory(File dir) { this.directory_ = dir; }
	
	public File getDirectory() { return this.directory_; }
	
	@Override
	public Boolean saveForm(IForm aForm) 
	{
		if(aForm instanceof IForm)
		{
			if(aForm.getId() == null)
				aForm.setId(RandomUniqueId.nextUniqueId());
			File file = this.getFileFormWithId(aForm.getId());
			Document root = this.formTransformer_.formToDocument(aForm, this.getImpl());
			if(root != null)
			{
				root.getDocumentElement().setAttribute("xmlns:bmb", "http://bmb.org");
				return this.serializeDocument(root, file);
			}
		}
		return false;
	}

	@Override
	public Boolean removeForm(IForm aForm) 
	{
		if(aForm instanceof IForm)
		{	
			File file = this.getFileFormWithId(aForm.getId());
			if(file != null)
				return file.delete();
		}
		return false;
	}

	@Override
	public IForm getFormWithId(String anId) 
	{
		File file = this.getFileFormWithId(anId);
		if(file != null)
		{
			return this.loadFormFromFile(file);
		}
		return null;
	}
	
	@Override
	public File getFileFormWithId(String anId)
	{
		File dir = this.getDirectory();
		if(dir != null)
		{
			return new File(dir, anId+".xml");
		}
		return null;
	}

	@Override
	public List<IForm> getAllForms() 
	{
		File dir = this.getDirectory();
		if(dir != null)
		{
			File[] files = dir.listFiles(new FilenameFilter() {
				@Override
				public boolean accept(File arg0, String arg1) {
					return arg1.endsWith(".xml");
				}
			});
			List<IForm> forms = new ArrayList<IForm>();
			for(int i=0; i<files.length; i++)
			{
				IForm form = this.loadFormFromFile(files[i]);
				if(form != null)
					forms.add(form);
			}
			return forms;
		}
		return null;
	}
	
	private IForm loadFormFromFile(File aFile)
	{
		if(aFile != null && aFile.exists())
		{
			Document root = this.parseFile(aFile);
			if(root != null)
			{
				return this.formTransformer_.formFromElement(root.getDocumentElement());
			}
		}	
		return null;
	}
}
